import { ref } from 'vue'
import {roleAdd,roleEdit} from '@/api/role';
import useInstance from '@/hooks/useInstance';
export const useRole = (getRoleList) => {
  const {global}  = useInstance()
  const roleAddAndEditRef = ref()
  const assignMenuRef = ref()
  const addBtn = ()=>{
    console.log('----------用户新增-------',roleAddAndEditRef.value);
    roleAddAndEditRef.value?.show('add')
  }
  const editBtn = (index,row)=>{
    roleAddAndEditRef.value?.show('edit',row)
  }
  const deleteBtn = (row)=>{}

  const save = async(params)=>{
    console.log('--------------------------');
    let res;
   if(params.action == 'add'){
    res  = await roleAdd(params)
   }
   if(params.action == 'edit'){
    res  = await roleEdit(params)
   }
   if(res && res.code == 200){
      getRoleList();
      global.$message({ message: res.msg, type: "success" })
   }
  }
  // 分配权限
  const assignPermission = (roleId,roleName) => {
    assignMenuRef.value?.show(roleId,roleName)
}
  
  return {
    roleAddAndEditRef,
    addBtn,
    editBtn,
    deleteBtn,
    save,
    assignPermission,
    assignMenuRef
  }
}